package com.d3works.apps.project.web;

import com.d3works.apps.base.db.Partner;
import com.d3works.apps.base.db.repo.PartnerRepository;
import com.d3works.apps.project.db.Project;
import com.d3works.apps.project.db.ProjectTemplate;
import com.d3works.apps.project.db.repo.ProjectTemplateRepository;
import com.d3works.apps.project.service.ProjectService;
import com.d3works.apps.project.service.ProjectTemplateService;
import com.d3works.exception.D3worksException;
import com.d3works.exception.service.TraceBackService;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import com.d3works.meta.schema.actions.ActionView;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.d3works.rpc.Context;
import com.google.inject.Singleton;
import java.util.LinkedHashMap;
import java.util.Map;

@Singleton
public class ProjectTemplateController {

  public void createProjectFromTemplate(ActionRequest request, ActionResponse response) {
    try {
      ProjectTemplate projectTemplate = request.getContext().asType(ProjectTemplate.class);
      Map<String, Object> projectTemplateView =
          Beans.get(ProjectService.class).createProjectFromTemplateView(projectTemplate);
      response.setView(projectTemplateView);
    } catch (D3worksException e) {
      TraceBackService.trace(response, e);
    }
  }

  @SuppressWarnings("unchecked")
  public void createProjectFromWizard(ActionRequest request, ActionResponse response) {

    Context context = request.getContext();

    String projectTemplateId =
        ((LinkedHashMap<String, Object>) context.get("_projectTemplate")).get("id").toString();
    ProjectTemplate projectTemplate =
        Beans.get(ProjectTemplateRepository.class).find(Long.parseLong(projectTemplateId));

    String projectCode = (String) context.get("code");

    Object clientPartnerContext = context.get("clientPartner");
    Partner clientPartner = null;

    if (clientPartnerContext != null) {
      String clientPartnerId =
          ((LinkedHashMap<String, Object>) clientPartnerContext).get("id").toString();
      clientPartner = Beans.get(PartnerRepository.class).find(Long.parseLong(clientPartnerId));
    }

    Project project;
    try {
      project =
          Beans.get(ProjectService.class)
              .createProjectFromTemplate(projectTemplate, projectCode, clientPartner);
      response.setCanClose(true);

      response.setView(
          ActionView.define(I18n.get("Project"))
              .model(Project.class.getName())
              .add("form", "project-form")
              .add("grid", "project-grid")
              .param("search-filters", "project-filters")
              .context("_showRecord", project.getId())
              .map());
    } catch (D3worksException e) {
      TraceBackService.trace(response, e);
    }
  }

  public void addParentTaskTemplate(ActionRequest request, ActionResponse response) {
    ProjectTemplate projectTemplate = request.getContext().asType(ProjectTemplate.class);
    projectTemplate =
        Beans.get(ProjectTemplateService.class).addParentTaskTemplate(projectTemplate);
    response.setValue("taskTemplateSet", projectTemplate.getTaskTemplateSet());
  }
}
